package cn.wolfcode.car.business.web.controller;


import cn.wolfcode.car.business.domain.ServiceItem;
import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.domain.StatementItem;
import cn.wolfcode.car.business.query.ServiceItemQuery;
import cn.wolfcode.car.business.query.StatemenItemQuery;
import cn.wolfcode.car.business.service.IServiceItemService;
import cn.wolfcode.car.business.service.IStatementItemService;
import cn.wolfcode.car.business.service.IStatementService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.web.AjaxResult;
import cn.wolfcode.car.shiro.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 控制器
 */
@Controller
@RequestMapping("business/statementItem")
public class StatementItemController {
    //模板前缀
    private static final String prefix = "business/statementItem/";

    @Autowired
    private IStatementItemService statementItemService;
    @Autowired
    private IStatementService statementService;


    //页面------------------------------------------------------------
    //列表

    /**
     * 点击结算单明细按钮跳转：
     * 1、消费中，跳转到明细编辑：editDetail
     * 2、已支付，跳转到明细查看：showDetail
     */
    @RequiresPermissions("business:statementItem:itemDetail")
    @RequestMapping("/itemDetail")
    public String itemDetail(Long statementId, Model model) {
        // 查询结算单的信息，
        Statement statement = statementService.get(statementId);
        model.addAttribute("statement", statement);
        // 如果是消费中的状态,跳转到明细编辑
        if (Statement.STATUS_CONSUME.equals(statement.getStatus())) {
            return prefix + "editDetail";
        } else {
            // 否则，已支付，跳转到明细查看
            statement.setPayee(ShiroUtils.getUser());
            return prefix + "showDetail";
        }
    }


    @RequiresPermissions("business:statementItem:view")
    @RequestMapping("/listPage")
    public String listPage() {
        return prefix + "list";
    }

    @RequiresPermissions("business:statementItem:add")
    @RequestMapping("/addPage")
    public String addPage() {
        return prefix + "add";
    }

    // 保存选中了的服务项
    @RequiresPermissions("business:statementItem:saveItems")
    @RequestMapping("/saveItems")
    @ResponseBody
    public AjaxResult saveItems(@RequestBody List<StatementItem> items) {
        statementItemService.saveItems(items);
        return AjaxResult.success();
    }

    // 点击确认用户已支付
    @RequiresPermissions("business:statementItem:payStatement")
    @RequestMapping("/payStatement")
    @ResponseBody
    public AjaxResult payStatement(Long statementId) {
        // 确定支付，修改statement的结算状态为已支付
        statementService.payStatement(statementId);
        return AjaxResult.success();
    }


    @RequiresPermissions("business:statementItem:edit")
    @RequestMapping("/editPage")
    public String editPage(Long id, Model model) {
        model.addAttribute("statementItem", statementItemService.get(id));
        return prefix + "edit";
    }

    //数据-----------------------------------------------------------
    //列表
    // 查看选了的服务项明细：
    @RequiresPermissions("business:statementItem:query")
    @RequestMapping("/query")
    @ResponseBody
    public TablePageInfo<StatementItem> query(StatemenItemQuery qo) {
        return statementItemService.query(qo);
    }

    //新增
    @RequiresPermissions("business:statementItem:add")
    @RequestMapping("/add")
    @ResponseBody
    public AjaxResult addSave(StatementItem statementItem) {
        statementItemService.save(statementItem);
        return AjaxResult.success();
    }

    //编辑
    @RequiresPermissions("business:statementItem:edit")
    @RequestMapping("/edit")
    @ResponseBody
    public AjaxResult edit(StatementItem statementItem) {
        statementItemService.update(statementItem);
        return AjaxResult.success();
    }

    //删除
    @RequiresPermissions("business:statementItem:remove")
    @RequestMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        statementItemService.deleteBatch(ids);
        return AjaxResult.success();
    }


}
